package com.my.main.common.encrypt;

import java.lang.reflect.Method;

public class Base64 {

	/***
	 * encode by Base64
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static String encode(byte[] input) {
		
		try {
			Class clazz = Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
			Method mainMethod = clazz.getMethod("encode", byte[].class);
			mainMethod.setAccessible(true);
			Object retObj = mainMethod.invoke(null, new Object[]{input});
			return (String)retObj;
		}
		catch(Exception e) {
			return input.toString();
		}
		
	}
	
	/***
	 * decode by Base64
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static byte[] decode(String input) {
		
		try {
			Class clazz = Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
			Method mainMethod = clazz.getMethod("decode", String.class);
			mainMethod.setAccessible(true);
			Object retObj = mainMethod.invoke(null, input);
			return (byte[])retObj;
		}
		catch(Exception e) {
			return input.getBytes();
		}
		
	}

}